/*
 * Created by Jaren at 2021/11/29 8:57
 */
import 'dart:typed_data';

import 'package:inmotion_protocol/common/compact.dart';
import 'package:inmotion_protocol/common/vehicle_info.dart';
import 'package:inmotion_protocol/lorin/constant.dart';
import 'package:inmotion_protocol/lorin/factory/special/special_message_factory.dart';
import 'package:inmotion_protocol/lorin/lorin_message.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

abstract class ScooterLorinSpecialMessageFactory<T extends ScooterCmdCompact>
    extends SpecialMessageFactory<T> {
  ScooterLorinSpecialMessageFactory(ProductProperty productProperty)
      : super(productProperty);

  LorinMessage genRequestTBoxEsimList() {
    return genSpecialOne2OneReadMsg(compact!.tBoxEsimListCmd(), null, null,
        false, null, Constant.deviceTBox);
  }

  LorinMessage genRequestTBoxRealTimeInfo() {
    return genSpecialOne2OneReadMsg(compact!.tBoxRealTimeInfoCmd(), null, null,
        false, null, Constant.deviceTBox);
  }

  LorinMessage genTBoxInfo() {
    return genSpecialOne2OneReadMsg(
        compact!.tBoxCmd(), null, null, false, null, Constant.deviceTBox);
  }

  LorinMessage genTBoxDeviceId() {
    return genSpecialOne2OneReadMsg(
        compact!.tBoxDeviceId(), null, null, false, null, Constant.deviceTBox);
  }

  LorinMessage genTBoxUserInfo() {
    return genSpecialOne2OneReadMsg(
        compact!.tBoxUserInfo(), null, null, false, null, Constant.deviceTBox);
  }

  LorinMessage genRebootTBox(int mode) {
    return genSpecialOne2OneWriteMsg(
        compact!.rebootTBoxCmd(),
        Uint8ClampedList.fromList([01, 00, mode]),
        null,
        false,
        null,
        Constant.deviceTBox);
  }

  LorinMessage genSetPasswordMsg(int type, String pwd) {
    return genSpecialMany2ManyWriteMsg(
        null,
        Constant.deviceLR1Display,
        compact!.passwordCmd(),
        ByteUtil.buildBytes(
            ByteUtil.num2BytesLe(type, 1),
            ByteUtil.num2BytesLe(pwd.length, 1),
            ByteUtil.pwd2Bytes(pwd, false)));
  }

  @override
  LorinMessage genHeadlightOnOffMsg(
      {bool? on, bool? lowBeamOn, bool? highBeamOn, int? source, int? target}) {
    return genSpecialOne2OneWriteMsg(compact!.headlightOnOffCmd(),
        Uint8ClampedList.fromList([on! ? 0xFF : 0]));
  }

  LorinMessage genCalibrateHandleMsg(int which) {
    return genSpecialOne2OneWriteMsg(
        compact!.calibrateCmd(), ByteUtil.num2BytesLe(which, 1));
  }

  /// 查询校准结果
  LorinMessage genQueryHandleCalibrationResultMsg(int which,
      [int? source, int? target]) {
    return genSpecialOne2OneReadMsg(
        compact!.calibrationResultCmd(), ByteUtil.num2BytesLe(which, 0));
  }

  LorinMessage genCruiseOnOffMsg(bool on) {
    return genSpecialOne2OneWriteMsg(
        compact!.cruiseCmd(), ByteUtil.bool2Bytes(on));
  }

  @override
  LorinMessage genSafeSpeedLimitOnOffMsg(bool onOff) {
    return genSpecialOne2OneWriteMsg(
        compact!.safeSpeedLimitCmd(), ByteUtil.bool2Bytes(onOff));
  }

  LorinMessage genZeroSpeedupOnOffMsg(bool on) {
    return genSpecialOne2OneWriteMsg(
        compact!.zeroSpeedupCmd(), ByteUtil.bool2Bytes(on));
  }

  LorinMessage genSetSpeedUnitMsg(int unit) {
    return genSpecialOne2OneWriteMsg(
        compact!.speedUnitCmd(), ByteUtil.num2BytesLe(unit, 1));
  }

  LorinMessage genTboxLowBatLevelWakeUpVehicleModeOnOffMsg (bool onOff) {
    return genSpecialOne2OneWriteMsg(
        compact!.tboxLowBatteryAwakeCmd(), ByteUtil.bool2Bytes(onOff));
  }
}

abstract class ScooterCmdCompact extends CmdCompact {
  @override
  int realTimeDataCmd() {
    return 1;
  }

  @override
  int realTimeStateCmd() {
    return 2;
  }

  @override
  int realTimeErrorCmd() {
    return 3;
  }

  @override
  int allRealTimeInfoCmd() {
    return 4;
  }

  @override
  int historyStatisticsCmd() {
    return 7;
  }

  @override
  int totalHistoryStatisticsCmd() {
    return 8;
  }

  @override
  int settingsCmd() {
    return 11;
  }

  int zeroSpeedupCmd() {
    return 12;
  }

  int cruiseCmd() {
    return 13;
  }

  int speedUnitCmd() {
    return 14;
  }

  @override
  int speedLimitCmd() {
    return 17;
  }

  @override
  int ridingModeCmd() {
    return 18;
  }

  @override
  int lockCmd() {
    return 19;
  }

  int safeSpeedLimitCmd() {
    return 22;
  }

  int shutdownCmd() {
    return 23;
  }

  @override
  int headlightOnOffCmd() {
    return 24;
  }

  int calibrateCmd() {
    return 25;
  }

  int tboxLowBatteryAwakeCmd() {
    return 25;
  }

  int calibrationResultCmd() {
    return 26;
  }

  int passwordCmd() {
    return 55;
  }
}
